﻿@using ESP.Tool
@{
    ViewBag.Title = "Index";
    string doMain = ESP.Tool.SystemDefinition.SystemSetting.GetAppSettingValue("domain");
    string ShortBaseUrl = ESP.Tool.SystemDefinition.SystemSetting.GetAppSettingValue("ShortBaseUrl");
}

<h2>Index</h2>
<table class="table table-bordered">
    <tr><td>原地址</td><td><input id="sourl" type="text" /></td><td><input type="button" value="生成" onclick="sub()" /></td></tr>
    <tr><td>短地址</td><td id="shurl"></td><td>二维码图<img id="shimg" style="width:120px; height:120px;" /></td></tr>
</table>
<br />
<input type="button" value="list" onclick="getList()" />
<table class="table table-bordered">
    <thead> <tr><td>短地址</td><td>原地址</td><td>二维码图</td></tr></thead>
<tbody id="tbodyList"></tbody>
</table>
<script>
    var ESPWebDomain = '@doMain';
    var ShortBaseUrl = '@ShortBaseUrl';
    function sub() {
        var surl = $('#sourl').val();
        $.ajax({
            type: "post",
            dataType: "json",
            data: { action: 'create', surl: surl },
            url: ESPWebDomain + "/shorturl/Ajax",
            async: true,
            success: function (result) {
                if (result != null) {
                    var sUrl = ShortBaseUrl + result.ShortUrl
                    $('#shurl').html(sUrl);
                    $('#shurl').attr("src", 'http://esp.ccnf.com/GeneratePic/QRCode?code=' + sUrl);
                }
            },
            error: function (e) {
                alert(JSON.stringify(e));
            }
        });
    }

    function getList()
    { 
        $.ajax({
            type: "GET",
            dataType: "json",
            data: { action: 'list' },
            url: ESPWebDomain + "/shorturl/Ajax",
            async: true,
            success: function (result) {
                if (result != null) {
                    var html = '';
                    var obj = JSON.parse(result);
                    $.each(obj, function (i, item) {
                        html += '<tr><td>' + item.ShortUrl + '</td><td>' + item.SourceUrl + '</td><td><img src="http://esp.ccnf.com/GeneratePic/QRCode?code=' + item.ShortUrl + '" style="width:120px; height:120px;" /></td></tr>';
                    });
                     
                    $('#tbodyList').html(html);
                }
            },
            error: function (e) {
                alert(JSON.stringify(e));
            }
        });

    }

</script>